﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using CrystalDecisions.CrystalReports.Engine;
using RM2012Server.Controller;
using RM2012Server.Business;
using CrystalDecisions.Shared;
using System.Windows;
using System.Drawing.Printing;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frptCustomer : Office2007Form
    {
        #region Fields
        CustomerCtrl m_CustomerCtrl = new CustomerCtrl();
        #endregion

        #region Constructor
        public frptCustomer()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frptCustomer_Load(object sender, EventArgs e)
        {
            cmbFormat.SelectedIndex = 0;
            lblZoom.Text = sliderZoom.Value.ToString() + " %";
            btnPrint.Visible = false;
            lblSave.Visible = false;
            lblZoom.Visible = false;
            sliderZoom.Visible = false;
            cmbFormat.Visible = false;
            btnSave.Visible = false;
            //
            btnFirst.Visible = false;
            btnPre.Visible = false;
            lblPageCurrent.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }
        #endregion

        #region Reports
        private void btnReport_Click(object sender, EventArgs e)
        {
            try
            {
                ParameterFields paramFields = new ParameterFields();
                //Parameter Full Name
                ParameterField field = new ParameterField();
                field.Name = "fullName";
                ParameterDiscreteValue pdv = new ParameterDiscreteValue();
                pdv.Value = GeneralInfo.fullName;
                field.CurrentValues.Add(pdv);
                //Parameter Report Code
                ParameterField field2 = new ParameterField();
                field2.Name = "codeReport";
                ParameterDiscreteValue pdv2 = new ParameterDiscreteValue();
                DateTime dateNow = DateTime.Now;
                String _year = dateNow.Year.ToString();
                String _month = dateNow.Month.ToString();
                if (dateNow.Month < 10)
                {
                    _month = "0" + dateNow.Month.ToString();
                }
                String _day = dateNow.Day.ToString();
                if (dateNow.Day < 10)
                {
                    _day = "0" + dateNow.Day.ToString();
                }
                String _hour = dateNow.Hour.ToString();
                if (dateNow.Hour < 10)
                {
                    _hour = "0" + dateNow.Hour.ToString();
                }
                String _minute = dateNow.Minute.ToString();
                if (dateNow.Minute < 10)
                {
                    _minute = "0" + dateNow.Minute.ToString();
                }
                String _second = dateNow.Second.ToString();
                if (dateNow.Second < 10)
                {
                    _second = "0" + dateNow.Second.ToString();
                }
                String codeReport = _year + _month + _day + _hour + _minute + _second;
                pdv2.Value = codeReport;
                field2.CurrentValues.Add(pdv2);
                //Add Parameter to List
                paramFields.Add(field);
                paramFields.Add(field2);
                //Add Parameter List to CrystalReport Viewer
                viewer.ParameterFieldInfo = paramFields;
                viewer.ReportSource = GetDocument();

                btnPrint.Visible = true;
                lblSave.Visible = true;
                lblZoom.Visible = true;
                sliderZoom.Visible = true;
                cmbFormat.Visible = true;
                btnSave.Visible = true;
                //
                btnFirst.Visible = true;
                btnPre.Visible = true;
                lblPageCurrent.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;
                //
                lblPageCurrent.Text = "1";
            }
            catch
            {
            	
            }
        }
        #endregion

        #region Public Method
        public String GetPath()
        {
            String path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));
            path = path + "Report/crptCustomer.rpt";
            return path;
        }

        public ReportDocument GetDocument()
        {
            ReportDocument doc = new ReportDocument();
            doc.Load(GetPath());
            doc.SetDataSource(GetSource());
            return doc;
        }

        public List<CustomerReportInfo> GetSource()
        {
            
            List<CustomerReportInfo> list = new List<CustomerReportInfo>();
            DataTable dt = m_CustomerCtrl.GetForReport();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CustomerReportInfo info = new CustomerReportInfo();
                    info.CusId = dt.Rows[i]["CusId"].ToString();
                    info.FullName = dt.Rows[i]["FullName"].ToString();
                    if (dt.Rows[i]["Sex"].ToString().Equals("") || dt.Rows[i]["Sex"] == null)
                    {
                        info.Sex = "Không rõ";
                    } 
                    else
                    {
                        if (Convert.ToBoolean(dt.Rows[i]["Sex"].ToString()) == true)
                        {
                            info.Sex = "Nữ";
                        }
                        else
                        {
                            info.Sex = "Nam";
                        }
                    }
                    info.Address = dt.Rows[i]["Address"].ToString();
                    info.Phone = dt.Rows[i]["Phone"].ToString();
                    list.Add(info);
                }
            }
            return list;
        }

        public void SaveFormat(ExportFormatType type, String fileName)
        {
            try
            {
                ReportDocument docSave = new ReportDocument();
                docSave = (ReportDocument)viewer.ReportSource;
                docSave.ExportToDisk(type, fileName);
            }
            catch
            {

            }
        }

        public void ZoomDocument(int level)
        {
            viewer.Zoom(level);
        }
        #endregion

        #region Event Handle
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //PDF
                String fileName = dialog.FileName;
                if (cmbFormat.SelectedIndex == 0)
                {
                    fileName = fileName + ".pdf";
                    SaveFormat(ExportFormatType.PortableDocFormat, fileName);
                }
                else if (cmbFormat.SelectedIndex == 1)
                {
                    //WORD
                    fileName = fileName + ".doc";
                    SaveFormat(ExportFormatType.WordForWindows, fileName);
                }
                else if (cmbFormat.SelectedIndex == 2)
                {
                    //EXCEL
                    fileName = fileName + ".xls";
                    SaveFormat(ExportFormatType.Excel, fileName);
                }
                else if (cmbFormat.SelectedIndex == 3)
                {
                    //HTML 40
                    fileName = fileName + ".html";
                    SaveFormat(ExportFormatType.HTML40, fileName);
                }
                MessageBoxEx.Show("File đã được lưu tới " + fileName, "LƯU BÁO CÁO");
            }
        }

        private void sliderZoom_ValueChanged(object sender, EventArgs e)
        {
            ZoomDocument(sliderZoom.Value);
            lblZoom.Text = sliderZoom.Value.ToString() + " %";
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            viewer.PrintReport();
        }
        #endregion

        #region Pages
        private void btnFirst_Click(object sender, EventArgs e)
        {
            viewer.ShowFirstPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            viewer.ShowPreviousPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
            if (viewer.GetCurrentPageNumber() == 1)
            {
                btnFirst.Enabled = false;
                btnPre.Enabled = false;
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            } 
            else
            {
                btnFirst.Enabled = true;
                btnPre.Enabled = true;
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            viewer.ShowNextPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            viewer.ShowLastPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
            btnFirst.Enabled = true;
            btnPre.Enabled = true;
            btnNext.Enabled = false;
            btnLast.Enabled = false;
        }
        #endregion
    }
}
